package mahjong;
import static org.junit.Assert.*;

import java.util.ArrayList;

import org.junit.*;

public class Milestone3Test {
	
	private Board testBoardTurtle, testBoardZig, testBoard;
	private Tile[] testTiles;
	private Viewer testViewer;
	private Player testPlayer;
	private HighScoreList hsl;
	
	@Before
	public void setUp()
	{
		String boardName = "Layouts"+ java.io.File.separator + "turtle.layout";
		testBoardTurtle =  new Board(boardName);
		boardName = "Layouts"+ java.io.File.separator + "ziggurat.layout";
		testBoardZig =  new Board(boardName);
		testTiles = testBoardTurtle.getContent();
		testPlayer = new Player();
		testViewer = new Viewer(testBoardTurtle, testPlayer ); 
		testPlayer.setBoard(testBoardTurtle);
		testPlayer.setView(testViewer);
		hsl = new HighScoreList("jUnitTest");
	}
	
	//test the heuristic player 1000 times and see win ratio with both layouts
	@Test
	public void heuristicSkill()
	{
		int wins = 0;
		testBoardTurtle.reset();
		for(int i =0; i<1000; i++){
			testBoardTurtle.autoPlay(1);
			if(testBoardTurtle.isEmpty()) wins++;
			testBoardTurtle.reset();
		}
		System.out.println("Turtle Wins(out of 1000) : " + wins + " efficiency= "+ wins/10 +"%");
		//assertTrue(wins>=500);
		wins = 0;
		testBoardZig.reset();
		for(int i =0; i<1000; i++){
			testBoardZig.autoPlay(1);
			if(testBoardZig.isEmpty()) wins++;
			testBoardZig.reset();
		}
		System.out.println("Zig Wins(out of 1000) : " + wins + " efficiency= "+ wins/10 +"%");
		//assertTrue(wins>=500);
	}
	
	/*Check if the move returned by the Random AI 
	 * is of two tiles of the same group and that
	 * they both can be removed.
	*/
	@Test
	public void testRandomMove(){
		RandomPlayer rPlayer = new RandomPlayer();
		rPlayer.init(testBoardTurtle);
		Tile[] move = rPlayer.chooseMove();
		assertTrue(move[0].getValue() == move[1].getValue());
		assertTrue(move[0].canRemove());
		assertTrue(move[1].canRemove());
	}
	/*Check if the move returned by the MonteCarlo AI 
	 * is of two tiles of the same group and that
	 * they both can be removed.
	*/
	@Test
	public void testMCMove(){
		MonteCarloPlayer mcPlayer = new MonteCarloPlayer();
		mcPlayer.init(testBoardTurtle);
		Tile[] move = mcPlayer.chooseMove();
		assertTrue(move[0].getValue() == move[1].getValue());
		assertTrue(move[0].canRemove());
		assertTrue(move[1].canRemove());
	}
	
	/*Check if the move returned by the Random AI 
	 * is of two tiles of the same group and that
	 * they both can be removed.
	*/
	@Test
	public void testHeuristicMove(){
		HeuristicPlayer hPlayer = new HeuristicPlayer();
		hPlayer.init(testBoardTurtle);
		Tile[] move = hPlayer.chooseMove();
		assertTrue(move[0].getValue() == move[1].getValue());
		assertTrue(move[0].canRemove());
		assertTrue(move[1].canRemove());
	}
	/*
	 * test if topFree correctly returns the that top is free
	 */
	@Test
	public void testTopFree(){
		Tile[] tiles = testBoardTurtle.getContent();
		//get tile a hidden tile and check if it is hidden
		assertFalse(tiles[25].isTopFree());
		//get a tile under the top centre tile and check that it is not top free
		assertFalse(tiles[tiles.length-2].isTopFree());
		//get top left tile and centre top tile and check they have top free
		assertTrue(tiles[tiles.length-1].isTopFree());
		assertTrue(tiles[0].isTopFree());
		
	}
	
	@Test
	public void testHeuristicTileSort(){
		ArrayList<Tile[]> moves = MoveOrderer.sortMoves(testBoardTurtle);
		int boardW = testBoardTurtle.getWidth();
		for(int i =1; i< moves.size(); i++ ){
			assertTrue(MoveOrderer.scoreTile(moves.get(i-1)[0],boardW) + MoveOrderer.scoreTile(moves.get(i-1)[1],boardW)
					   >= MoveOrderer.scoreTile(moves.get(i)[0],boardW) +MoveOrderer.scoreTile(moves.get(i)[1],boardW));
		}
		
	}
	

	
}
